package leetcode;

import java.util.*;

public class LeetCodeMain107 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA==null||headB==null){
            return null;
        }
        int countA=listNodeSize(headA);
        int countB=listNodeSize(headB);
        if (countA>countB){
            int count=countA-countB;
            while (count>0&&headA!=null){
                count--;
                headA=headA.next;
            }
        }
        if (countA<countB){
            int count=countB-countA;
            while (count>0&&headB!=null){
                count--;
                headB=headB.next;
            }
        }
        while (headA!=null&&headB!=null){
            if (headA==headB){
                return headA;
            }
            headA=headA.next;
            headB=headB.next;
        }
        return null;
    }
    public int listNodeSize(ListNode head){
        ListNode n1=head;
        int count=0;
        while (n1!=null){
            count++;
            n1=n1.next;
        }
        return count;
    }
    public boolean verifyPostorder(int[] postorder) {
        Stack<Integer> stack = new Stack<>();
        int root = Integer.MAX_VALUE;
        for(int i = postorder.length - 1; i >= 0; i--) {
            if(postorder[i] > root) return false;
            while(!stack.isEmpty() && stack.peek() > postorder[i])
                root = stack.pop();
            stack.add(postorder[i]);
        }
        return true;
    }
    public int maxValue(int[][] grid) {
        for (int i = 1; i < grid.length; i++) {
            grid[i][0]+=grid[i-1][0];
        }
        for (int i = 1; i < grid[0].length; i++) {
            grid[0][i]+=grid[0][i-1];
        }
        for (int i = 1; i < grid.length; i++) {
            for (int j = 1; j < grid[0].length; j++) {
                grid[i][j]+=Math.max(grid[i-1][j],grid[i][j-1]);
            }
        }
        return grid[grid.length-1][grid[0].length-1];
    }
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (B==null||A==null){
            return false;
        }
        return isSubStructure(A.left,B)||isSubStructure(A.right,B)||inOrder(A,B);
    }
    public boolean inOrder(TreeNode A,TreeNode B){
        if (B==null){
            return true;
        }
        if (A==null){
            return false;
        }
        return A.val==B.val&&inOrder(A.left,B.left)&&inOrder(A.right,B.right);
    }
    public boolean isSubStructure1(TreeNode A, TreeNode B) {
        if (B==null){
            return false;
        }
        PriorityQueue<TreeNode> queue=new PriorityQueue<>();
        if (A!=null){
            queue.offer(A);
        }
        while (!queue.isEmpty()){
            int size=queue.size();
            while (size-->0){
                TreeNode root=queue.poll();
                if (root.left!=null){
                    queue.offer(root.left);
                }if (root.right!=null){
                    queue.offer(root.right);
                }
                if (root.val!=B.val){
                    continue;
                }
            }
        }
        return false;
    }
}
